#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver
import  time
import os

def PCDescibeMainFun(browser,filePath):
    #写入PC端的描述
    PCWriteDescription(browser,filePath)

    #滑动鼠标
    time.sleep(2)
    js="var q=document.documentElement.scrollTop=2000"
    browser.execute_script(js)

    #写入手机端的描述
    PhoneWriteDescription(browser,filePath)

def PCWriteDescription(browser,filePath):
    #点击富文本
    clickRichTextBox(browser)

    #点击富文本按钮上传图片
    clickRichTextButton(browser)

    #TO DO
    # detailImgCount = len(os.listdir(r"C:\Users\Administrator\Desktop\imgxq"))
    detailImgCount = len(os.listdir(r"C:\Users\hesy\Desktop\img"))

    # 启动AutoIt来控制文件
    callAutoItEXE();

    cehcekUploadedIMG(browser,detailImgCount);





def PhoneWriteDescription(browser,filePath):
    browser.find_element_by_xpath('//*[@id="struct-wirelessDesc"]/div/div[2]/div[1]/div/div/div[2]/div[1]/button').click()

    pass

#点击富文本框
def clickRichTextBox(browser):
    time.sleep(5)
    browser.find_element_by_xpath('//*[@id="struct-descType"]/div/div[2]/div[1]/div/span/span[1]/label').click()

    browser.find_element_by_xpath('//*[@id="cke_1_contents"]/div').click()
    elementText = browser.find_element_by_id('cke_1_contents')

    webdriver.ActionChains(browser).move_to_element(elementText ) \
        .send_keys('偏远地区不包邮，补邮后在发货(新疆，西藏，内蒙古等)，详情请咨询客服~ ') \
        .perform()

#点击富文本上传图片按钮
def clickRichTextButton(browser):
    time.sleep(5)
    element = browser.find_element_by_id("cke_56")
    webdriver.ActionChains(browser).move_to_element(element ).click(element ).perform()
    #点击子页面的上传图片按钮
    browser.switch_to_window(browser.window_handles[0])
    time.sleep(5)
    frame =browser.find_element_by_xpath('/html/body/div[14]/div/div[2]/div/iframe')
    browser.switch_to.frame(frame)
    time.sleep(2)
    browser.find_elements_by_xpath('//*[@id="container"]/div/div[2]/div[2]/div/div[1]/div[2]/div[1]/div[5]')[0].click()
    #点击上传
    browser.find_element_by_id('uploadBtn').click()

def cehcekUploadedIMG(browser,detailImgCount):
    time.sleep(60)
    indexs = detailImgCount

    for index in range(detailImgCount):
        time.sleep(1)
        browser.find_element_by_xpath('//*[@id="items"]/div['+str(indexs)+']').click()
        indexs = indexs-1

    #这个按钮是确认图片确定的按钮
    browser.find_element_by_xpath('//*[@id="container"]/div/div[2]/div[2]/div/div[2]/div[1]').click()
    browser.switch_to.default_content()


def callAutoItEXE():
    os.system("F:\soft\MeSoft\AutoIT\AutoItWorks\MainIMG.exe")